$(function() {
	// setup the dialog
	$("#timeout-dialog").dialog({
		autoOpen : false,
		modal : true,
		closeOnEscape : false,
		draggable : true,
		resizable : true,
		buttons : {
			"Ok" : function() {
				$(this).dialog('close');
				onTouch();
			}
		}
	});
});

// idle options
timer_options = {
	timeout : '1200s',
	countdown : 21,
	time_out_url: "/account/logout/",
};

function onTouch() {
	$("#dialog-countdown").stopTime('countdown-writer');
	$("#timeout-dialog").stopTime('coutdown-executer');
	$("#dialog-countdown").html(timer_options.countdown);
	//Dajaxice.chat.touch(function () {},{});
	startIdleSensor();
}

function onIdle() {
	stopIdleSensor();
	$("#timeout-dialog").dialog("open");
	// countdown
	$("#dialog-countdown").html(timer_options.countdown);
	$("#dialog-countdown").everyTime(1000, 'countdown-writer', function(i) {
		$(this).html(timer_options.countdown - i);
	}, timer_options.countdown);
	// on time out - logout
	$("#timeout-dialog").oneTime(timer_options.countdown * 1000, 'coutdown-executer',
			function() {
				$(document).stopTime('idle');
				window.location = timer_options.time_out_url;
			})
}

function startIdleSensor() {
	$(document).everyTime(timer_options.timeout, 'idle', onIdle, 0)
}

function stopIdleSensor() {
	$(document).stopTime('idle')
}
